package cn.lyq.card.ui.aboutus

import androidx.lifecycle.Observer
import cn.lyq.card.BuildConfig
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.src.Status
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_about_us.*

class AboutUsActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(AboutUsVM::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_about_us
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_about_us)
        tvVersion.text = "版本号 ${BuildConfig.VERSION_NAME}"
    }

    override fun initListener() {
        super.initListener()
        tvCheckUpdate.setClick {
            getVersion()
        }
    }

    private fun getVersion() {
        mModel.getApp().observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    if (BuildConfig.VERSION_NAME != it.requireData().version) {
                        NewVersionFg.newInstance(it.requireData()).show(this)
                    } else {
                        ToastUtils.showShort(R.string.str_current_new)
                    }
                    dismissLoading()
                }
                Status.ERROR -> {
                    dismissLoading()
                    ToastUtils.showShort(it.message.getMsg())
                }
            }
        })
    }

}